<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Http\Requests\CheckoutUserInfoRequest;
use App\Model\Front\Adress;
use Gloudemans\Shoppingcart\Facades\Cart;
use Illuminate\Http\Request;

class CheckoutController extends Controller
{
    public function index()
    {
        $carts = Cart::instance('shopping')->content();
        if (!$carts->isEmpty()) {
            $userAdress = Adress::where('user_id',auth()->id())->get();
            return view('front.checkout', compact('carts'))
                        ->withSubtotal(Cart::subtotal())
                        ->withuserAdress($userAdress);
        } else {
            return response()->view('errors.403', ['message'=>'没有任何东西需要结算.'], 403);
            // abort(500,'Null Content');
        }
    }

    public function store(CheckoutUserInfoRequest $request)
    {
        $adress = Adress::create([
                'country'=>$request->country,
                'state'=>$request->state,
                'city'=>$request->city,
                'pincode'=>$request->pincode,
                'fullname'=>$request->fullname,
                'user_id'=>auth()->id()
            ]);
        if ($adress) {
            return back();
        }
    }
}
